<?php

class DateWithoutDayValidator extends sfValidatorDate {

    protected function convertDateArrayToString($value) {

        // all elements must be empty or a number
        foreach (array('year', 'month') as $key) {
            if (isset($value[$key]) && !preg_match('#^\d+$#', $value[$key]) && !empty($value[$key])) {
                throw new sfValidatorError($this, 'invalid', array('value' => $value));
            }
        }

        // if one date value is empty, all others must be empty too
        $empties =
                (!isset($value['year']) || !$value['year'] ? 1 : 0) +
                (!isset($value['month']) || !$value['month'] ? 1 : 0)
        ;
        if ($empties > 0 && $empties < 2) {
            throw new sfValidatorError($this, 'invalid', array('value' => $value));
        } else if (2 == $empties) {
            return $this->getEmptyValue();
        }

        if (!checkdate(intval($value['month']), intval(01), intval($value['year']))) {
            throw new sfValidatorError($this, 'invalid', array('value' => $value));
        }


        $clean = sprintf(
                        "%04d-%02d-%02d %02d:%02d:%02d",
                        intval($value['year']),
                        intval($value['month']),
                        intval(01),
                        0,
                        0,
                        0
        );


        return $clean;
    }

}